package com.ssy.lingxi.marketing.serviceimpl.component;

import com.ssy.lingxi.common.model.bo.MemberAndRoleIdBO;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.marketing.service.feign.IMemberFeignService;
import com.ssy.lingxi.member.merchant.api.model.vo.inner.request.MemberAndUpperMembersVO;
import com.ssy.lingxi.member.merchant.api.model.vo.inner.request.MemberFeignVO;
import com.ssy.lingxi.member.merchant.api.model.vo.inner.response.AtSubMemberSuitableMemberVO;
import com.ssy.lingxi.member.merchant.api.model.vo.inner.response.PfSubMemberSuitableMemberVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 营销查询会员关系组件
 * @author yzc
 * @version 2.0.0
 * @date 2021/9/28
 */
@Slf4j
@Component
public class SuitableMemberInfoComponent {
    @Resource
    private IMemberFeignService memberFeignService;

    /**
     * 查询能力中心会员适用等级信息
     * @param memberId 会员id
     * @param roleId 角色id
     * @param memberAndRoleIdBOList 上级会员角色
     * @return 适用用会员等级信息
     */
    public List<AtSubMemberSuitableMemberVO> listAbilitySubMemberSuitableMember(Long memberId, Long roleId, List<MemberAndRoleIdBO> memberAndRoleIdBOList) {
        MemberAndUpperMembersVO memberAndUpperMembersVO = new MemberAndUpperMembersVO();
        memberAndUpperMembersVO.setSubMemberId(memberId);
        memberAndUpperMembersVO.setSubRoleId(roleId);

        List<MemberFeignVO> memberFeignVOList = memberAndRoleIdBOList.stream().distinct().map(e -> {
            MemberFeignVO memberFeignVO = new MemberFeignVO();
            memberFeignVO.setMemberId(e.getMemberId());
            memberFeignVO.setRoleId(e.getRoleId());
            return memberFeignVO;
        }).collect(Collectors.toList());
        memberAndUpperMembersVO.setUpperMemberRoles(memberFeignVOList);

        // 查询会员关系
        Wrapper<List<AtSubMemberSuitableMemberVO>> suitableMemberVOListWrapper = memberFeignService.listAbilitySubMemberSuitableMember(memberAndUpperMembersVO);
        if (ResponseCode.SUCCESS.getCode() != suitableMemberVOListWrapper.getCode()) {
            log.error("校验适用会员,查询会员服务失败errCode: {}, errMsg: {}", suitableMemberVOListWrapper.getCode(), suitableMemberVOListWrapper.getMessage());
            return null;
        }

        return suitableMemberVOListWrapper.getData();
    }

    /**
     * 查询平台后台会员适用等级信息
     * @param memberId 会员id
     * @param roleId 角色id
     * @return 适用用会员等级信息
     */
    public PfSubMemberSuitableMemberVO getPlatformSubMemberSuitableMember(Long memberId, Long roleId) {
        MemberFeignVO memberFeignVO = new MemberFeignVO();
        memberFeignVO.setMemberId(memberId);
        memberFeignVO.setRoleId(roleId);
        // 查询会员关系
        Wrapper<PfSubMemberSuitableMemberVO> suitableMemberVOWrapper = memberFeignService.getPlatformSubMemberSuitableMember(memberFeignVO);
        if (ResponseCode.SUCCESS.getCode() != suitableMemberVOWrapper.getCode()) {
            log.error("校验适用会员,查询会员服务失败errCode: {}, errMsg: {}", suitableMemberVOWrapper.getCode(), suitableMemberVOWrapper.getMessage());
            return null;
        }
        return suitableMemberVOWrapper.getData();
    }
}
